﻿//
// File: DeleteOperation.cs
//
// Purpose:  implements a queued delete operation
// Author: MotherHacker @ cachecleaner.codeplex.com


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using QueuedOperation;
using Reporting;

namespace CacheCleaner
{
    public class DeleteOperation : AsyncOperation
    {
        public FileSystemInfo Target;

        public DeleteOperation(FileSystemInfo target)
        {
            this.Target = target;

        }

        /// <summary>
        /// Performs the delete operation.
        /// </summary>
        public override void Run()
        {
            //if (null != Target)
            //    return;

            try
            {

                if (Target.Exists)
                    Target.Delete();

                ////
                // ReportingService.WriteEvent("OK", "File '" + Target.FullName + "' Deleted.");

            }
            catch (UnauthorizedAccessException accessdenied)
            {
                ReportingService.WriteEvent("FAIL", "Deleting file '" + Target.FullName + "' Failed, Reason: Access Denied", accessdenied.Message);
            }
            catch (FileNotFoundException notfound)
            {
                ReportingService.WriteEvent("FAIL", "Deleting file '" + Target.FullName + "' Failed, Reason: File not found", notfound.Message);
            }
            catch (PathTooLongException toolong)
            {
                ReportingService.WriteEvent("FAIL", "Deleting file '" + Target.FullName + "' Failed, Reason: Path too long", toolong.Message);

            }
            catch ( Exception x )
            {
                ReportingService.WriteEvent("FAIL", "Deleting file '" + Target.FullName + "' Failed", x.Message);

            }
        }
    }
}
